const A = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    isFilterType: false,
    isFilterArea: false,
    isFilterMask: false,
    business: '',
    area: '',
    businessType: '',
    areaType: '',
    filterType: [{"dictValue":"","dictLabel":"全部"}],
    filterArea: [{"dictValue":"","dictLabel":"全部"}],
    shopList: []
  },
  // 弹出选择状态和地区
  onFilterbar(e) {
    let type = A.C(e).type;
    if (type == 1) {
      this.setData({
        isFilterType: true,
        isFilterMask: true,
        isFilterArea: false
      })
    } else {
      this.setData({
        isFilterType: false,
        isFilterMask: true,
        isFilterArea: true
      })
    }
  },
  // 选择状态
  onFilterType(e) {
    let value = A.C(e).value;
    let label = A.C(e).label;
    this.setData({
      isFilterType: false,
      isFilterMask: false,
      isFilterArea: false,
      businessType: value,
      business: label,
      shopList:[],
      pageNum:1
    })
    this.getMerchants();
  },
  // 选择地区
  onFilterArea(e) {
    let value = A.C(e).value;
    let label = A.C(e).label;
    this.setData({
      isFilterType: false,
      isFilterMask: false,
      isFilterArea: false,
      areaType: value,
      area: label,
      shopList:[],
      pageNum:1
    })
    this.getMerchants();
  },

  //
  onFilterMask() {
    this.setData({
      isFilterType: false,
      isFilterMask: false,
      isFilterArea: false
    })
  },

  // 获取商家列表
  getMerchants() {
    let config = {
      businessType: this.data.businessType,
      areaType: this.data.areaType,
      pageNum: this.data.pageNum++,
      pageSize: 10
    }
    A.updata.getMerchants(config).then(res => {
      let shopList = this.data.shopList;
      this.setData({
        shopList: shopList.concat(res.list)
      })
    })
  },

  // 跳转商家详情
  toShopInfo(e) {
    let goodId = A.C(e).id;
    wx.navigateTo({
      url: `/pages/mall/goodsDetail/shopInfo/shopInfo?id=${goodId}`
    })
  },

  // 查询区域
  getAreaType() {
    let config = {
      dictType: 'region_type'
    }
    A.updata.getdictData(config).then(res => {
      let that = this;
      this.setData({
        filterArea: that.data.filterArea.concat(res.list),
        area: that.data.filterArea[0].dictLabel,
        areaType: that.data.filterArea[0].dictValue
      });
      this.getMerchants();
      console.log(this.data.filterArea);
    })
  },

  // 查询行业
  getBusinessType() {
    let config = {
      dictType: 'merchant_type'
    }
      let that = this;
    A.updata.getdictData(config).then(res => {
      this.setData({
        filterType: that.data.filterType.concat(res.list),
        business: that.data.filterType[0].dictLabel,
        businessType: that.data.filterType[0].dictValue
      });

      this.getAreaType();
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBusinessType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      this.getMerchants();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})